/**
 * zmn渠道服务工程师-列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author xiangjunming
 * @since 2019/10/16 18:07
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-140'
    });
    laypage.render(zmnPage.laypageOptions(json));

    // 省市联动
    form.on('select(provinceId)', function(obj){
        triggerCityOptions(obj.value);
    })

    function triggerCityOptions(value){
        let cityOptions = json.citys.filter(p => p.parentId == value).map(p => {
            return p.areaId == json.cityId ? '<option value="'+p.areaId+'" selected="selected">'+p.name+'</option>' : '<option value="'+p.areaId+'">'+p.name+'</option>';
        });
        cityOptions.unshift('<option value=""></option>');
        $('#cityId').html(cityOptions);
        form.render("select");
    }

    if(json.provinceId){
        console.log('triggerCityOptions')
        triggerCityOptions(json.provinceId);
    }


    //【表格左上侧工具栏监听】
    table.on("toolbar(zmn-table)", function (obj) {
        if(obj.event == "del") {
            var ids = zmnPage.layTabldIds(obj, "id");
            if(ids.length === 0){ return layer.alert("请选择至少一条数据！"); }
            layer.confirm('确认移除工程师吗？', function() {
                zmnPage.ajaxSimple("/channel/servicemaster/del", { ids: ids.join(",")}, 1000);
            });
            return;
        }

        if (obj.event == "selectWhiteList") {
            zmnLayUtil.openIframeCustom(layer, "/channel/servicemaster/select.action?pageSize=50&pageNum=1&channelId="+json.channelId+"&type=2"+"&addFlag="+json.addFlag, {
                title: '新增白名单',
                area: ['60%', '60%']
            });
            return;
        }

        if (obj.event == "selectBlackList") {
            zmnLayUtil.openIframeCustom(layer, "/channel/servicemaster/select.action?pageSize=50&pageNum=1&channelId="+json.channelId+"&type=1"+"&addFlag="+json.addFlag, {
                title: '新增黑名单',
                area: ['60%', '60%']
            });
            return;
        }
    });

    //【表格右侧工具栏监听】
    table.on('tool(zmn-table)', function (obj) {
        var id = obj.data.id;
        if(obj.event == "del") {
            layer.confirm('确认移除工程师吗？', function() {
                zmnPage.ajaxSimple("/channel/servicemaster/del", { ids: id}, 1000);
            });
            return;
        }
    });

    //对外暴露的接口
    exports('channel_servicemaster_list', {});
});